package org.example.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.common.PageResult;
import org.example.pojo.entity.TbStudent;
import org.example.enums.AppHttpCodeEnum;
import org.example.exception.LeadException;
import org.example.mapper.TbStudentMapper;
import org.example.service.TbStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Service
public class TbStudentServiceImpl extends ServiceImpl<TbStudentMapper, TbStudent> implements TbStudentService {
    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 学生信息excel表格下载
     * @param response
     * @throws IOException
     */
    @Override
    public void download(HttpServletResponse response) throws IOException {
        List<TbStudent> list = this.list();
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        EasyExcel.write(response.getOutputStream(), TbStudent.class).sheet("学生信息").doWrite(list);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<TbStudent> getStudents(Long page, Long pageSize) {
        //构造分页
        Page<TbStudent> iPage = new Page<>(page, pageSize);
        Page<TbStudent> studentPage = this.page(iPage);
        //判断是否为空
        if (CollectionUtils.isEmpty(studentPage.getRecords())) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return new PageResult<>(page, pageSize, studentPage.getTotal(), studentPage.getRecords());
    }



    /**
     * 根据名字查询学生信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageResult<TbStudent> getStudentsList(Long page, Long pageSize, String name) {
        //构造分页
        LambdaQueryWrapper<TbStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(TbStudent::getSname,name);
        Page<TbStudent> iPage = new Page<>(page, pageSize);
        Page<TbStudent> studentPage = this.page(iPage,queryWrapper);
        //判段是否为空
        if (CollectionUtils.isEmpty(studentPage.getRecords())) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return new PageResult<>(page, pageSize, studentPage.getTotal(), studentPage.getRecords());
    }



    /**
     * 修改学生信息
     * @param student
     */
    @Override
    public void editStudent(TbStudent student) {
        this.updateById(student);
    }

    /**
     * 添加学生
     * @param student
     */
    @Override
    public void addStudent(TbStudent student) {
        this.save(student);
    }

    /**
     * 删除学生信息
     * @param sid
     */
    @Override
    public void deleteStudent(Integer sid) {
        this.removeById(sid);
    }


}
